
innerapp.controller('server_watcher_Ctrl', ['$scope', '$timeout', '$http','$route', 
    function ($scope, $timeout, $http,$route) {
        /*$scope.$on('$viewContentLoaded', function () { 
            loadScript("../assets/plugins/bootstrap4-toggle/js/bootstrap4-toggle.min.js")
         });*/
        var timeTick = 3000;
        $scope.daemonRunning = true;
        $scope.protocolRunning = true;
        $scope.mqttRunning = true;
        $scope.webRunning = true;
        $scope.useServiceControl=false;
        var timer = function () {
            if($route.current.loadedTemplateUrl != "views/server_watcher.html"){
                return;
            }; 
            console.log($route.current.loadedTemplateUrl);
            $http.get("/Service/swsm/getStatues/")
                .then(function (result) {
                    if (result.data.res != null && typeof (result.data.res) == "object") {
                        $scope.daemonRunning = Boolean(result.data.res.daemon);
                        $scope.protocolRunning = Boolean(result.data.res.protocol);
                        $scope.mqttRunning = Boolean(result.data.res.mqtt);
                        $scope.webRunning = Boolean(result.data.res.web);
                    } else {
                        console.log("getAllStatue=error");
                    }
                });
            $timeout(timer, timeTick);
        };
        $http.get("/Service/swsm/getServerOS/")
            .then(function (result) {
                if (result.data != null && typeof (result.data) == "string")
                    if(result.data.toUpperCase().includes("WINDOWS")){
                        $timeout(timer, timeTick);
                        $scope.useServiceControl = true;
                    }
            });
        $scope.damonClick = function () {
            $http.get("/Service/swsm/setDaemon/?control=" + (!$scope.daemonRunning))
                .then(function (result) {
                    if (result.data != null && typeof (result.data) == "string" && result.data == "Success") {
                        console.log("setDaemon=" + $scope.daemonRunning + " success");
                    } else {
                        console.log(result.data);
                    }
                });
        };
        $scope.protocolClick = function () {
            if (!$scope.daemonRunning) {
                $http.get("/Service/swsm/controlProcess/?control=" + (!$scope.protocolRunning) + "&processName=ProtocolServer")
                    .then(function (result) {
                        if (result.data != null && typeof (result.data) == "string" && result.data == "Success") {
                            console.log("controlProcess protocol=" + $scope.protocolRunning + " success");
                        } else {
                            console.log(result.data);
                        }
                    });
            }
        };
        $scope.mqttClick = function () {
            if (!$scope.daemonRunning) {
                $http.get("/Service/swsm/controlProcess/?control=" + (!$scope.mqttRunning) + "&processName=EmqxServer")
                    .then(function (result) {
                        if (result.data != null && typeof (result.data) == "string" && result.data == "Success") {
                            console.log("controlProcess mqtt=" + $scope.mqttRunning + " success");
                        } else {
                            console.log(result.data);
                        }
                    });
            }
        };
        $scope.webClick = function () {
            if (!$scope.daemonRunning) {
                $http.get("/Service/swsm/controlProcess/?control=" + (!$scope.webRunning) + "&processName=WebServer")
                    .then(function (result) {
                        if (result.data != null && typeof (result.data) == "string" && result.data == "Success") {
                            console.log("controlProcess web=" + $scope.webRunning + " success");
                        } else {
                            console.log(result.data);
                        }
                    });
            }
        };
    }]);



